/*
 * @lc app=leetcode.cn id=303 lang=cpp
 *
 * [303] 区域和检索 - 数组不可变
 */
#include "include.h"
// @lc code=start

// 什么lazy计算都是些垃圾，反倒更慢了~
class NumArrayLazy {
public:
    NumArrayLazy(vector<int>& nums) {
        numsPtr_ = &nums;
        sumVec_.reserve(nums.size());
        sumVec_[0] = nums[0];
        alreadyDoneIndex_ = 0;
    }
    int alreadyDoneIndex_;
    vector<int> sumVec_;
    vector<int>* numsPtr_;
    int sumRange(int left, int right) {
        if (alreadyDoneIndex_ < right){
            for (int i=alreadyDoneIndex_+1;i<=right;i++){
                sumVec_[i] = sumVec_[i-1] + (*numsPtr_)[i];
            }
            alreadyDoneIndex_ = right;
        }
        return sumVec_[right] - sumVec_[left] + (*numsPtr_)[left];
    }
};

class NumArray {
public:
    vector<int> sums;

    NumArray(vector<int>& nums) {
        int n = nums.size();
        sums.resize(n + 1);
        for (int i = 0; i < n; i++) {
            sums[i + 1] = sums[i] + nums[i];
        }
    }

    int sumRange(int i, int j) {
        return sums[j + 1] - sums[i];
    }
};

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray* obj = new NumArray(nums);
 * int param_1 = obj->sumRange(left,right);
 */
// @lc code=end

